

import java.awt.*;
import java.awt.event.*;
import java.sql.Date;
import javax.swing.*;

public class EquipamentoCRUDForm {
    private static EquipamentoCRUDControl control = new EquipamentoCRUDControl();
    private static JPanel pData, pCusto, pSaida;
    private static JTextField cod, data, custo, cpfSaida, nomeSaida;
    private static JButton button;
    private static JComboBox op;
    private static JPanel pPreco;
    private static JTextField preco;
    private static JComboBox tipo;
    private static JPanel pEixos;
    private static JTextField eixos;
    
    public EquipamentoCRUDForm() {
    }
    
    public Panel novoForm(){
        Panel form = new Panel();
        form.setLayout(new FlowLayout());
       
        JPanel panel = new JPanel(new GridLayout(9,1));
        form.add(panel);
      
        String[] array = new String[4];
        array[0] = "Cadastrar Equipamento";
        array[1] = "Remover Equipamento";
        array[2] = "Editar Equipamento";
        array[3] = "Visualizar Equipamento";
        JComboBox choice = new JComboBox(array);
        op = choice;
        panel.add(choice);
        EquipamentoCRUDForm.ChoiceListener itemListener = new EquipamentoCRUDForm.ChoiceListener() {};
        choice.addItemListener(itemListener);
      
        JPanel row = new JPanel();
        JLabel label = new JLabel("Código: "); // construct Label
        row.add(label);
        JTextField text = new JTextField("0000", 10);
        cod = text;
        row.add(text);
        panel.add(row);

        row = new JPanel();
        pData = row;
        label = new JLabel("Data: "); // construct Label
        row.add(label);
        text = new JTextField("dd/mm/yyyy", 10);
        data = text;
        row.add(text);
        panel.add(row);
      
        row = new JPanel();
        pCusto = row;
        label = new JLabel("Custo de manter: "); // construct Label
        row.add(label); 
        text = new JTextField("R$0,00", 10);
        custo = text;
        row.add(text);
        panel.add(row);
        
        row = new JPanel();
        pPreco = row;
        label = new JLabel("Preco: "); // construct Label
        row.add(label);
        text = new JTextField("R$0,00", 10);
        preco = text;
        row.add(text);
        panel.add(row);
        
        array = new String[4];
        array[0] = "Caminhão";
        array[1] = "Balança";
        array[2] = "Prensa";
        array[3] = "Escritório";
        choice = new JComboBox(array);
        tipo = choice;
        panel.add(choice);
        EquipamentoCRUDForm.ChoiceListener2 itemListener2 = new EquipamentoCRUDForm.ChoiceListener2() {};
        choice.addItemListener(itemListener2);
        
        row = new JPanel();
        pEixos = row;
        label = new JLabel("Número de eixos: "); // construct Label
        row.add(label); 
        text = new JTextField("4", 10);
        eixos = text;
        row.add(text);
        panel.add(row);

        button = new JButton("Cadastrar Equipamento"); // construct Button
        panel.add(button);
        EquipamentoCRUDForm.ButtonListener buttonListener = new EquipamentoCRUDForm.ButtonListener();
        button.addActionListener(buttonListener);

        row = new JPanel();
        pSaida = row;
        text = new JTextField(10);
        text.setEditable(false);
        cpfSaida = text;
        row.add(text);
        text = new JTextField(10);
        text.setEditable(false);
        nomeSaida = text;
        row.add(text);
        panel.add(row);
        pSaida.setVisible(false);
 
        form.setSize(300, 300);
        form.setVisible(true);
        return form;
    }
    
    private static class ChoiceListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent ie) {
            button.setLabel(op.getSelectedItem().toString());
            switch (op.getSelectedIndex()) {
                case 0:
                    pData.setVisible(true);
                    pCusto.setVisible(true);
                    pPreco.setVisible(true);
                    tipo.setVisible(true);
                    pEixos.setVisible(true);
                    pSaida.setVisible(false);
                    break;
                case 1:
                    pData.setVisible(false);
                    pCusto.setVisible(false);
                    pPreco.setVisible(false);
                    tipo.setVisible(false);
                    pEixos.setVisible(false);
                    pSaida.setVisible(false);
                    break;
                case 2:
                    pData.setVisible(true);
                    pCusto.setVisible(true);
                    pPreco.setVisible(true);
                    tipo.setVisible(true);
                    pEixos.setVisible(true);
                    pSaida.setVisible(false);
                    break;
                case 3:
                    pData.setVisible(false);
                    pCusto.setVisible(false);
                    pPreco.setVisible(false);
                    tipo.setVisible(false);
                    pEixos.setVisible(false);
                    pSaida.setVisible(true);
                    break;
            }
        }
    }
    
    private static class ChoiceListener2 implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent ie) {
            switch (tipo.getSelectedIndex()) {
                case 0:
                    pEixos.setVisible(true);
                    break;
                case 1:
                    pEixos.setVisible(false);
                    break;
                case 2:
                    pEixos.setVisible(false);
                    break;
                case 3:
                    pEixos.setVisible(false);
                    break;
            }
        }
    }
    
    private static class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String codigo = cod.getText();
            
            switch (op.getSelectedIndex()) {
                case 0:
                    Date c = new Date(Date.parse(data.getText()));
                    String n = custo.getText();
                    String p = preco.getText();
                    int t = tipo.getSelectedIndex();
                    int e = Integer.parseInt(eixos.getText());
                    control.create(codigo, c, n, p, t, e);
                    break;
                case 1:
                    control.delete(codigo);
                    break;
                case 2:
                    control.update(codigo);
                    break;
                case 3:
                    control.read(codigo);
                    break;
            }
        }
    }
   
}